package org.yuanqiframework.yuanqi.core.utils;

import java.util.Random;
import java.util.UUID;

/**
 * 随机字符串工具类
 *
 * @author 廖半仙
 */
public class RandomUtils {

    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 获得从a-z，长度为length的随机数
     */
    public static String getRandomStr(int length) {
        String letters = "abcdefghijklmnopqrsduvwxyz";
        Random random = getRandom();
        StringBuilder bf = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int anInt = random.nextInt(letters.length());
            bf.append(letters.charAt(anInt));
        }
        return bf.toString();
    }

    /**
     * 获得从a-z0-9，长度为length的随机字符
     */
    public static String getRandomString(int length) {
        String letters = "abcdefghijklmnopqrsduvwxyz0123456789";
        Random random = getRandom();
        StringBuilder bf = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int anInt = random.nextInt(letters.length());
            bf.append(letters.charAt(anInt));
        }
        return bf.toString();
    }

    /**
     * 获得length长度的随机数字字符串
     */
    public static String getRandomNum(int length) {
        String nums = "0123456789";
        Random random = getRandom();
        StringBuilder bf = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int anInt = random.nextInt(nums.length());
            bf.append(nums.charAt(anInt));
        }
        return bf.toString();
    }


    private static Random getRandom() {
        return new Random();
    }
}
